/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UploadFile.java
 *
 * Created on Aug 15, 2011, 2:36:59 PM
 */
package panels;

import com.zehon.FileTransferStatus;
import com.zehon.exception.FileTransferException;
import com.zehon.ftp.FTP;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author jm1156
 */
public class UploadFile extends javax.swing.JPanel {

    /** Creates new form UploadFile */
    public UploadFile() {
        initComponents();
    }
    
    public boolean uploadFile() {
        String host = "ftp.emis-redbull.com";
        String username = "ftp_user@emis-redbull.com";
        String password = "ftp_user";
        String destFolder = "/";
        try {
            String filePath = m_txtFileUpload.getText();
            int status = FTP.sendFile(filePath, destFolder, host, username, password);
            if (FileTransferStatus.SUCCESS == status) {
                System.out.println(filePath + " got ftp-ed successfully to  folder " + destFolder);
            } else if (FileTransferStatus.FAILURE == status) {
                System.out.println("Fail to ftp  to  folder " + destFolder);
                return false;
            }
        } catch (FileTransferException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        m_txtFileUpload = new javax.swing.JTextField();
        m_btnBrowse = new javax.swing.JButton();
        m_btnUpload = new javax.swing.JButton();

        m_txtFileUpload.setName("m_txtFileUpload"); // NOI18N

        m_btnBrowse.setText("Browse...");
        m_btnBrowse.setName("m_btnBrowse"); // NOI18N
        m_btnBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_btnBrowseActionPerformed(evt);
            }
        });

        m_btnUpload.setText("Upload");
        m_btnUpload.setName("m_btnUpload"); // NOI18N
        m_btnUpload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_btnUploadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(m_btnUpload, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(m_txtFileUpload, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(m_btnBrowse)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(m_txtFileUpload, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(m_btnBrowse))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(m_btnUpload)
                .addContainerGap(22, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void m_btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_btnBrowseActionPerformed
        // TODO add your handling code here:
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setVisible(true);
        int returnVal = fileChooser.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            if(!selectedFile.exists()) {
                JOptionPane.showMessageDialog(null, "Unknown file: "+selectedFile.getAbsolutePath());
                return;
            }
            m_txtFileUpload.setText(selectedFile.getAbsolutePath());
            
            
        }
    }//GEN-LAST:event_m_btnBrowseActionPerformed

    private void m_btnUploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_btnUploadActionPerformed
        // TODO add your handling code here:
        if(uploadFile()) {
            JOptionPane.showMessageDialog(null, "Successfully uploaded data!");
            m_txtFileUpload.setText("");
        } else {
            JOptionPane.showMessageDialog(null, "Unable to upload data.");
        }
    }//GEN-LAST:event_m_btnUploadActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton m_btnBrowse;
    private javax.swing.JButton m_btnUpload;
    private javax.swing.JTextField m_txtFileUpload;
    // End of variables declaration//GEN-END:variables
}
